<?php
/**
 * Description of StringLength
 *
 * @author      Maciej Zawieja.
 * @link        http://akvus.pl
 * @package     Akvus Framework.
 * @subpackage  Libraries.
 * @copyright   2011 All rights reserved for Maciej Zawieja.
 * @version     1.0.0 - 19.08.2011 (Framework 1.3)
 */
class StringLength implements iValidation {

    const PARAMS_QUANTITY = 2;

    private $min;

    private $max;

    public function isValid($text) {
        $length = strlen($text);

        if( $this->min > $this->max || $this->max < $this->min )
            throw new ValidationException ('Max can not be greater than min.', ValidationException::PARAMS_DISCREPANT);

        if ( $length < $this->min )
            throw new ValidationException ('String is too short', ValidationException::TO_SHORT);

        if( $length > $this->max )
            throw new ValidationException ('String is too long', ValidationException::TO_LONG);
    }

    /**
     *
     * @param array $params One param - $max, two params - $min, $max.
     */
    public function setParams( array &$params ) {
        if ( count($params) == self::PARAMS_QUANTITY ) {
            $this->min = $params[0];
            $this->max = $params[1];
        }
        else if ( count($params) == self::PARAMS_QUANTITY - 1 ) {
            $this->min = 0;
            $this->max = $params[0];
        } else throw new ProgrammerException ('Too less params for RegEx validation.', 0);
    }

    function Max( $max = null )
    {
        if ($max === null) return $this->max;
        $this->max = $max;
        return $this;
    }

    function Min( $min = null )
    {
        if ($min === null) return $this->min;
        $this->min = $min;
        return $this;
    }
}